package com.ruoyi.project.gxgw_modules.systemConfig.controller;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.gxgw_modules.systemConfig.domain.SystemConfig;
import com.ruoyi.project.gxgw_modules.systemConfig.service.ISystemConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 系统配置 信息操作处理
 *
 * @author ruoyi
 * @date 2019-01-22
 */
@Controller
@RequestMapping("/gxgw_modules/systemConfig")
public class SystemConfigController extends BaseController {
    private String prefix = "gxgw_modules/systemConfig";

    @Autowired
    private ISystemConfigService systemConfigService;

    @RequiresPermissions("gxgw_modules:systemConfig:view")
    @GetMapping()
    public String systemConfig() {
        return prefix + "/systemConfig";
    }

    /**
     * 查询系统配置列表
     */
    @RequiresPermissions("gxgw_modules:systemConfig:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SystemConfig systemConfig) {
        startPage();
        List<SystemConfig> list = systemConfigService.selectSystemConfigList(systemConfig);
        return getDataTable(list);
    }

    /**
     * 导出系统配置列表
     */
    @RequiresPermissions("gxgw_modules:systemConfig:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SystemConfig systemConfig) {
        List<SystemConfig> list = systemConfigService.selectSystemConfigList(systemConfig);
        ExcelUtil<SystemConfig> util = new ExcelUtil<SystemConfig>(SystemConfig.class);
        return util.exportExcel(list, "systemConfig");
    }

    /**
     * 新增系统配置
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存系统配置
     */
    @RequiresPermissions("gxgw_modules:systemConfig:add")
    @Log(title = "系统配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SystemConfig systemConfig) {
        systemConfig.setSyscLogo("无");
        return toAjax(systemConfigService.insertSystemConfig(systemConfig));
    }

    /**
     * 修改系统LOGO
     */
    @GetMapping("/editLogo/{syscId}")
    public String editLogo(@PathVariable("syscId") Integer syscId, ModelMap mmap) {
        SystemConfig systemConfig = systemConfigService.selectSystemConfigById(syscId);
        mmap.put("systemConfig", systemConfig);
        return prefix + "/avatar";
    }

    /**
     * 保存logo
     */
    @PostMapping("/updateLogo/{syscId}")
    @RequiresPermissions("gxgw_modules:systemConfig:editLogo")
    @ResponseBody
    public AjaxResult updateAvatar(@RequestParam("logo") MultipartFile file, @PathVariable("syscId") Integer syscId) {
        try {
            if (!file.isEmpty()) {
                String filename = FileUploadUtils.upload(RuoYiConfig.getProfile() + "logo/", file);
                SystemConfig systemConfig = new SystemConfig();
                systemConfig.setSyscId(syscId);
                systemConfig.setSyscLogo(filename);
                if (systemConfigService.updateSystemConfig(systemConfig) > 0) {

                    return success();
                }
            }
            return error();
        } catch (Exception e) {

            return error(e.getMessage());
        }

    }

    /**
     * 修改系统配置
     */
    @GetMapping("/edit/{syscId}")
    public String edit(@PathVariable("syscId") Integer syscId, ModelMap mmap) {
        SystemConfig systemConfig = systemConfigService.selectSystemConfigById(syscId);
        mmap.put("systemConfig", systemConfig);
        return prefix + "/edit";
    }

    /**
     * 修改保存系统配置
     */
    @RequiresPermissions("gxgw_modules:systemConfig:edit")
    @Log(title = "系统配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SystemConfig systemConfig) {
        return toAjax(systemConfigService.updateSystemConfig(systemConfig));
    }

    /**
     * 删除系统配置
     */
    @RequiresPermissions("gxgw_modules:systemConfig:remove")
    @Log(title = "系统配置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(systemConfigService.deleteSystemConfigByIds(ids));
    }

}
